package com.ws.dz.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ws.dz.mapper.user.entity.SysUser;
import com.ws.dz.mapper.user.mapper.SysUserMapper;
import com.ws.rain.base.result.R;
import com.ws.rain.base.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "WebController", description = "系统接口")
public class WebController {
    private SysUserMapper userMapper;

    @Autowired
    WebController(SysUserMapper userMapper){
        this.userMapper = userMapper;
    }

    @PostMapping("/login/failure")
    @ApiOperation("登录失败，回调接口")
    public R loginFailure() {
        return R.fail(ResultCode.UN_AUTHORIZED, "登录失败");
    }


    @PostMapping("/login/success")
    @ApiOperation("登录成功，回调接口")
    public R loginSuccess() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        ew.eq("username",username);
        SysUser sysUser = userMapper.selectList(ew).get(0);
        sysUser.setPassword("[PROTECT]");
        return R.data(sysUser);
    }

    @PostMapping("/logout/success")
    @ApiOperation("退出成功，回调接口")
    public R logoutSuccess() {
        return R.data("退出登录成功");
    }

}
